/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Thanhpho;
import bc.util.HibernateUtil;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Welcome
 */
public class ThanhphoDAO {
     private Session session;

    public ThanhphoDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }

    public Thanhpho[] getAllThanhpho()  {     
        try {
            session.getTransaction().begin();
            String sql = "from Thanhpho";
            Query query = session.createQuery(sql);
            List result=query.list();
           Thanhpho[] cityList = new Thanhpho[result.size()];
            result.toArray(cityList);
            session.flush();
            session.getTransaction().commit();
            return cityList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;

    }
    //cua nga
    public void save(Thanhpho tp) {
        try {
            session.getTransaction().begin();
            session.save(tp);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    //cua nga
    public void delete(int id) {
        try {
            session.getTransaction().begin();
            session.delete(session.get(Thanhpho.class, id));

            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    //cuanga
    public boolean checkdelete(int id) {
        boolean result = false;
        try {
            session.getTransaction().begin();
            session.delete(session.get(Thanhpho.class, id));
            session.flush();
            //  session.getTransaction().commit();
            ThanhphoDAO tpdao = new ThanhphoDAO();
            Thanhpho lh = tpdao.getThanhphoById(id);
            if (lh == null) {
                result = true;
            } else {
                result = false;
            }

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return result;
    }
     //cua nga

    public List<Thanhpho> getThanhphoLikeName(String name) {
        try {
            session.getTransaction().begin();
            String sql = "from Thanhpho where thanhpho like ?";
            Query query = session.createQuery(sql);
            query.setString(0, "%" + name + "%");
            List rs = query.list();

            session.flush();
            return rs;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();

        }
        return null;
    }

    //cua nga
    public Thanhpho getThanhphoById(int id) {
        session.beginTransaction().begin();
        String sql = "from Thanhpho where id_thanhpho=?";
        try {
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            Thanhpho tp = (Thanhpho) query.uniqueResult();
            session.flush();
            return tp;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        ThanhphoDAO A=new ThanhphoDAO();
        System.out.println("huyen");
        Thanhpho[] B=A.getAllThanhpho();
        for(int i=0;i<B.length;i++){
            System.out.println("a ... "+B[i].getThanhpho());
        }
    }
}
